const express = require('express');
const path = require('path');
const { createProxyMiddleware: proxy } = require('http-proxy-middleware');
const fallback = require('express-history-api-fallback');

const app = express();

const root = path.join(__dirname, '../dist');

// 开启静态资源中间件
// __dirname 当前文件所在目录的绝对路径
// path.join(__dirname, '../dist')) 会拼接成一个绝对路径
app.use(express.static(root));

app.use(
  '/api',
  proxy({
    target: 'http://hrm.itheima.net',
    changeOrigin: true,
    pathRewrite: {
      '^/api': '/prod-api',
    },
  })
);

app.use(fallback('index.html', { root }));

app.listen(3000, () => {
  console.log('Server started at http://localhost:3000/');
});
